<?php
/*
 * @Author: an
 * @Date: 2021/12/10
 * @Description: 融云即时通讯基类
 */


namespace App\Services\RongCloud;


use App\Services\Configs\DefaultService;

class BaseService
{
    protected $mainHost = 'https://api-cn.ronghub.com';//主域名
    protected $prepareHost = 'https://api2-cn.ronghub.com';//备域名
    protected $appKey;//添加应用时获取
    protected $appSecret;//添加应用时获取

    public function __construct()
    {
        //获取配置
        $config = $this->getConfig(DefaultService::RONGYUN_IM);
        $this->appKey = $config['appKey'];
        $this->appSecret = $config['appSecret'];
    }

    /**
     * 获取请求头
     * @return array
     * @author an
     * @time 2021-12-10 14:54
     */
    protected function getHeaders()
    {
        // 重置随机数种子。
        srand((double)microtime() * 1000000);
        $nonce = rand(); // 获取随机数。
        $timestamp = time() * 1000; // 获取时间戳（毫秒）。
        //生成签名
        $signature = sha1($this->appSecret . $nonce . $timestamp);
        $headers = [
            'App-Key:' . $this->appKey,
            'Nonce:' . $nonce,
            'Signature:' . $signature,
            'Timestamp:' . $timestamp,
            'Content-Type:application/x-www-form-urlencoded'
        ];
        return $headers;
    }

    /**
     * 校验回调签名是否正确
     * 仅针对即时通讯相关回调服务有效，
     * 不包括实时音视频相关回调
     * @return bool
     * @author an
     * @time 2021-12-10 14:14
     */
    protected function callbackSign()
    {
        $nonce = $_GET['nonce']; // 获取随机数。
        $timestamp = $_GET['signTimestamp']; // 获取时间戳。
        $signature = $_GET['signature']; // 获取数据签名。
        $local_signature = sha1($this->appSecret . $nonce . $timestamp); // 生成本地签名。
        if (strcmp($signature, $local_signature) === 0) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 获取配置信息
     * @param $key 配置 key
     * @return array
     * @throws \Exception
     * @author an
     * @time 2021-12-10 14:05
     */
    protected function getConfig($key)
    {
        $config = new DefaultService();
        $data = $config->first($key);
        if (!$data) {
            throw new \Exception('找不到配置 ' . $key);
        }
        return $data;
    }

    /**
     * HTTP 请求
     * @param $url 资源路径
     * @param $bodys 请求参数
     * @param $headers 请求头
     * @param string $method 请求方法
     * @return bool|string
     * @author an
     * @time 2021-12-10 14:04
     */
    protected function request($url, $bodys, $headers, $method = 'POST')
    {
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($curl, CURLOPT_FAILONERROR, false);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_HEADER, false);
        if (1 == strpos("$" . $url, "https://")) {
            curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        }
        curl_setopt($curl, CURLOPT_POSTFIELDS, $bodys);
        $data = curl_exec($curl);
        return $data;
    }
}
